package com.dfire.soa.mcsao.life;


import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@Slf4j
public class LifeCycleConfig implements BeanPostProcessor{

    private final static Logger LOGGER = LoggerFactory.getLogger(LifeCycleConfig.class);
    private static int size = 0;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        log.warn( "BeanPostProcessor 初始化 {},{}",size++ + beanName);

        return bean;
    }


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }


    /**
     * 单例创建一个类
     * @return
     */
    @Bean(initMethod = "start", destroyMethod = "destroy")
    public SpringLifeCycle create(){
        SpringLifeCycle springLifeCycle = new SpringLifeCycle() ;
        log.warn("SpringLifeCycle init");
        return springLifeCycle ;
    }

    @Bean(initMethod = "init", destroyMethod = "PreDestroy")
    public PostUtil create2(){
        PostUtil postUtil = new PostUtil() ;
        log.warn("postUtil init");
        return postUtil ;
    }
}
